//
//  MYGCommonMaskView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/16.
//

import UIKit

class MYGCommonMaskView: UIView {
    // MARK: - Property
    var maskColor: UIColor? {
        didSet {
            self.backgroundColor = maskColor
        }
    }
    var maskViewTappedAction: (()->())?
    
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        addGestureRecognizer(tapGes)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Public funcs
    public func maskViewTapped() {
        maskViewTappedAction?()
    }
    
    // MARK: - lazy load
    private lazy var tapGes: UITapGestureRecognizer = {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapGestrueTriggered))
        tap.delegate = self
        return tap
    }()
}

extension MYGCommonMaskView: UIGestureRecognizerDelegate  {
    @objc func tapGestrueTriggered() {
        maskViewTapped()
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if gestureRecognizer == tapGes && touch.view != tapGes.view {
            return false
        }
        return true
    }
    
    
}
